package com.supreme.proxyserver;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;

import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Optional;

@Slf4j
@EnableFeignClients
@ComponentScan(value = {"com.supreme.proxyserver", "com.supreme.commonboot", "com.supreme.configcloud", "com.supreme.commonmybatis"})
@SpringBootApplication
public class SupremeProxyServerApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SupremeProxyServerApplication.class, args);
        final String applicationName = applicationContext.getApplicationName();
        System.out.printf("\n\n\t\t ↖(▔▽▔)↗ SupremeProxyServerApplication %s 启动完成 ↖(▔▽▔)↗ \n\n", applicationName);
    }

    @Component
    static class ProxyServerRunner implements ApplicationRunner {

        @Override
        public void run(ApplicationArguments args) throws Exception {
            try {
                String classpath = ResourceUtils.getURL("classpath:").getPath();
                System.out.println("classpath = " + classpath);
            } catch (FileNotFoundException e) {
                System.out.println(e.getMessage());
            }

            final ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader();
            Optional.ofNullable(defaultClassLoader)
                    .map(classLoader -> classLoader.getResource(""))
                    .map(URL::getPath)
                    .ifPresent(s -> System.out.println("classpath = " + s));
        }
    }
}
